package cn.bby.tools.utils;

import cn.hutool.core.util.StrUtil;
import com.intellij.openapi.actionSystem.AnActionEvent;
import com.intellij.openapi.actionSystem.CommonDataKeys;
import com.intellij.openapi.application.WriteAction;
import com.intellij.openapi.command.CommandProcessor;
import com.intellij.openapi.editor.Editor;
import com.intellij.openapi.editor.SelectionModel;
import com.intellij.openapi.project.Project;
import com.intellij.psi.PsiFile;
import com.intellij.psi.PsiJavaFile;
import org.jetbrains.annotations.NotNull;

import java.util.Optional;

/**
 * @author ben
 * @since 2025/9/24 20:13
 */
public class AnActionEventUtils {
    /// 获取选中的文本
    public static String getSelectedText(@NotNull AnActionEvent e) {
        return Optional.ofNullable(e.getData(CommonDataKeys.EDITOR))
                .map(editor -> {
                    SelectionModel selectionModel = editor.getSelectionModel();
                    if (selectionModel.hasSelection(true)) {
                        // 如果有列选择，则获取列选择的文本
                        return selectionModel.getSelectedText(true);
                    } else {
                        // 否则返回普通选择的文本
                        return selectionModel.getSelectedText();
                    }
                })
                .orElse(StrUtil.EMPTY);
    }

    /// 替换选中的文本
    public static void replaceSelection(@NotNull AnActionEvent e, String newText) {
        Optional.ofNullable(e.getData(CommonDataKeys.EDITOR))
                .ifPresent(editor -> CommandProcessor.getInstance().executeCommand(
                        editor.getProject(),
                        () -> WriteAction.run(() -> {
                            SelectionModel selectionModel = editor.getSelectionModel();
                            editor.getDocument().replaceString(
                                    selectionModel.getSelectionStart(),
                                    selectionModel.getSelectionEnd(),
                                    newText
                            );
                        }),
                        "Replace Selection",
                        null
                ));
    }

    /// 获取当前文件
    public static PsiFile getCurrentFile(@NotNull AnActionEvent e) {
        Project project = e.getProject();
        if (project == null) {
            return null;
        }

        Editor editor = e.getData(CommonDataKeys.EDITOR);
        if (editor == null) {
            return null;
        }

        return e.getData(CommonDataKeys.PSI_FILE);
    }


    public static String getCurrentLine(@NotNull AnActionEvent e) {
        Project project = e.getProject();
        if (project == null) {
            return StrUtil.EMPTY;
        }

        Editor editor = e.getData(CommonDataKeys.EDITOR);
        if (editor == null) {
            return StrUtil.EMPTY;
        }

        int caretOffset = editor.getCaretModel().getOffset();
        int lineNumber = editor.getDocument().getLineNumber(caretOffset);
        return editor.getDocument().getText().substring(
                editor.getDocument().getLineStartOffset(lineNumber),
                editor.getDocument().getLineEndOffset(lineNumber)
        );
    }
}